%%%-------------------------------------------------------------------
%% @doc crontab top level supervisor.
%% @end
%%%-------------------------------------------------------------------

-module(crontab_sup).

-behaviour(supervisor).

-export([start_link/0]).

-export([init/1]).

-define(SERVER, ?MODULE).

start_link() ->
    supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%% sup_flags() = #{strategy => strategy(),         % optional
%%                 intensity => non_neg_integer(), % optional
%%                 period => pos_integer()}        % optional
%% child_spec() = #{id => child_id(),       % mandatory
%%                  start => mfargs(),      % mandatory
%%                  restart => restart(),   % optional
%%                  shutdown => shutdown(), % optional
%%                  type => worker(),       % optional
%%                  modules => modules()}   % optional
init([]) ->
    {ok, Files} = application:get_env(crontab, files),
    SupFlags = 
        #{ strategy => one_for_one
         , intensity => 5
         , period => 10
         },
    ChildSpecs = 
        [ pack_normal_worker(crontab_timer, [dummy])
        , pack_normal_worker(crontab_trigger, [Files])
        ],
    {ok, {SupFlags, ChildSpecs}}.


%% internal functions
pack_normal_worker(Mod, Args) ->
    #{ id       => Mod
     , start    => {Mod, start_link, Args}
     , restart  => permanent
     , shutdown => 1000
     , type     => worker
     , modules  => [Mod]
     }.